package com.example.sistemacompras.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sistemacompras.R;
import com.example.sistemacompras.entity.Client;

public class UserListAdapter extends ArrayAdapter<Client> {
    private int textViewResourceId;

    private List<Client> objects;

    public UserListAdapter(Context context, int textViewResourceId, List<Client> objects) {
        super(context, textViewResourceId, objects);
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(textViewResourceId, null);
        Client client = objects.get(position);
        if (client != null) {
            ImageView image = (ImageView) row.findViewById(R.id.user_image);
            if(client.getPhoto()!=null)image.setImageBitmap(client.getPhoto());

            TextView userName = (TextView) row.findViewById(R.id.user_name);
            userName.setText(client.getName());
        }

        return row;
    }
}
